<?php
/**
 * 旅人蕉生活网 电影部分 调用 满天星接口 数据处理代码
 * @author LSH 2013-02-17 
 * 
 * 注意：全局的实例化接口         getInstance  
 *      
 *          
 *          Mtx_MovieApi::getInstance()->generalGet($para_list);
 *
 */
final class Mtx_MovieApi
 {
	
	private static $m_instance;    // 定义一个静态的变量，为类的实例
	
	private $apiUrl = "http://partner.okticket.cn:8012/TicketAPI.asmx"; // 接口位置..
	//private $apiUrl = "http://localhost:8080/test/test4.php"; // 接口位置..
// 	private $apiUrl = "http://localhost/kernel/libaray/test4.php"; // 接口位置..
	private $pAppCode  = CONF_MTX_PAPPCODE;  // 应用编码
	private $pAppPwd   = CONF_MTX_KEY;  //  验证密钥
	private $opt             = array();      // POST 配置参数数组 ..
	private $apiToUrl    = "";
	
	private $pVerifyInfo = "";  // 校验信息
	
	private $xml = null;   // simpleXML 解析类
	
	private $m_to_para = array();
	private $m_to_string ="";
	
	
	private static $func_list = array(
			'GetCinema' => 1,
			'GetCinemaPlan' => 1,
			
			
			
			'RealCheckSeatState' => 0,
			
			);
		
	private function __construct() // 重载构造函数，为private 这样外部就不能创建了。
	{
		$this->xml = new SimpleXML();
		
	}
	
	public static function getInstance() // 唯一一个获取实例的外部接口
	{
		if (self::$m_instance == NULL)
		{
			self::$m_instance = new Mtx_MovieApi();
		}
		
		return self::$m_instance;
	}
	
	
	public function __clone()  //创建__clone方法防止对象被复制克隆
	{
		trigger_error('Clone is not allow!',E_USER_ERROR);
	}

	
	
	
	/**
	 * post 电影接口
	 * @param array $para_list  参数列表
	 */
	private function post()
	{		
		foreach ($this->m_to_para as $k =>$v)
		{
			$this->m_to_string .= $k.'='.$v.'&';
		}
		
		$this->m_to_string = substr($this->m_to_string, 0,strlen($this->m_to_string) -1);
		
		$xml_data = getHttpResponsePOST($this->apiToUrl,$this->opt,$this->m_to_string);
		
 		//echo $xml_data;
 		//exit;
			

		$this->m_to_para = array();
		$this->m_to_string = "";
		
		
		$xml = simplexml_load_string($xml_data);
		
		return $this->xml->xml2array($xml);
	}
	
	
	
	/**
	 * 校验信息的生成方法..
	 * @param array $para_list 参数名 => 值..
	 * @return string 返回校验信息..
	 */
	private function getMd5Str($para_list = array())
	{
		$str_long = '';
		
		if (is_array($para_list) && !empty($para_list))
		{
			foreach ($para_list as $k => $v) 
			{
				$str_long.=$v;				
			}
		}

		// TODO:中间还有很多判断没有做 例如 参数是xml,参数字符类型，需要等到确定规则之后再补充...
		
		$str_long .= $this->pAppPwd;
		
		//  校验信息=转换成小写（MD5（转换成小写（应用编码+参数1+参数2+…..+验证密钥）））
		return strtolower(substr(md5(strtolower($str_long)), 8,16));
		
		
	}
	
	/**
	 * 暂时废除 不用 ...
	 * @param string $func         需要调用的接口名称
	 * @param array $para_list   所需要的参数列表  
	 * @return array                    返回php 数组
	 * 
	 * eg. generalGet('GetCinema',array());
	 */
	public function generalGet($func,$para_list = array())
	{
		$to_para = array();
		if (!isset(self::$func_list[$func]))
		{
			return false;
		}	
		
	    $this->apiToUrl = $this->apiUrl. "/".$func;
	    
	    if ((self::$func_list[$func] === 0))
	    {
	    	foreach ($para_list as $k => $v)
	    	{
	    		$to_para[$k] = $this->xml->xml_encode($v);
	    	}
	    }
	    else
	    {
	    	$to_para = $para_list;
	    }
	    
		return $this->post($to_para);
	}
	
	 // ------------------------------------------------------------  以下是对外开发的调用接口 ---------------------------------------
	
	
	/**
	 * @param array $para_list
	 * {
	 * 	null
	 *  }
	 * @return 
	 */
	public function getCinema($para_list = array())  // 4.1  获取电影院信息
	{
		$this->apiToUrl = $this->apiUrl."/GetCinema";
		
		$this->m_to_para['pAppCode'] = $this->pAppCode;
		

		$this->m_to_para['pVerifyInfo'] = $this->getMd5Str($this->m_to_para);
		$ret =  $this->post();
		
		return $ret;
		
	}
	
	/**
	 * @param array $para_list
	 * {
	 * 	pCinemaID	String[10字节]	电影院编号
	 *		pPlanDate	String[10字节]	获取排期的日期 格式(yyyy-mm-dd)
	 * }
	 * @return 
	 */
	public function getCinemaPlan($para_list = array()) //  4.2  获取对应影院对应日期的排期
	{
		$this->apiToUrl = $this->apiUrl. "/GetCinemaPlan";
		$this->m_to_para['pAppCode'] = $this->pAppCode;
		
		$this->m_to_para['pCinemaID'] = $para_list['pCinemaID'];
		$this->m_to_para['pPlanDate']  = $para_list['pPlanDate'];
		
		$this->m_to_para['pVerifyInfo'] = $this->getMd5Str($this->m_to_para);
		
		$ret =  $this->post($this->m_to_para);
		
		return $ret;
	}
	
	/**
	 * @param array $para_list
	 * {
	 * 	pCinemaID	String[10字节]	电影院编号
	 * }
	 * @return 
	 */
	public function getCinemaAllPlan($para_list = array()) //  4.3  获取对应影院所有可读排期
	{
		$this->apiToUrl = $this->apiUrl. "/GetCinemaAllPlan";
		$this->m_to_para['pAppCode'] = $this->pAppCode;
		
		$this->m_to_para['pCinemaID'] = $para_list['pCinemaID'];
		
		$this->m_to_para['pVerifyInfo'] = $this->getMd5Str($this->m_to_para);
		$ret =  $this->post($this->m_to_para);
	
		return $ret;
	}
	
	/**
	 * @param array $para_list
	 * {
	 * 	pCinemaID	String[10字节]	电影院编号
	 * }
	 * @return 
	 */
	public function getHall($para_list = array()) //  4.4  获取影院对应的影厅信息
	{
		$this->apiToUrl = $this->apiUrl. "/GetHall";
		$this->m_to_para['pAppCode'] = $this->pAppCode;
		
		$this->m_to_para['pCinemaID'] = $para_list['pCinemaID'];
		
		$this->m_to_para['pVerifyInfo'] = $this->getMd5Str($this->m_to_para);
		
		$ret =  $this->post($this->m_to_para);
		
		//exit;
		return $ret;
	}
	
	/**
	 * @param array $para_list
	 * {
	 * 	pCinemaID	String[10字节]	电影院编号
	 *		pHallID	    String[2字节]	影厅编号
	 * }
	 * @return 
	 */
	public function getHallSite($para_list = array()) //  4.5  获取影厅对应的所有座位信息
	{
		$this->apiToUrl = $this->apiUrl. "/GetHallSite";
		$this->m_to_para['pAppCode'] = $this->pAppCode;
		
		$this->m_to_para['pCinemaID'] = $para_list['pCinemaID'];
		$this->m_to_para['pHallID']       = $para_list['pHallID'];
		
		$this->m_to_para['pVerifyInfo'] = $this->getMd5Str($this->m_to_para);
		$ret =  $this->post($this->m_to_para);
	
		return $ret;
	}
	
	/**
	 * @param array $para_list
	 * {
	 * 	pCinemaID	String[10字节]	电影院编号
	 *		pHallID	    String[2字节]	影厅编号
	 * }
	 * @return 
	 */
	public function getHallSiteArea($para_list = array()) //  4.6  获取影厅对应的所有座区信息
	{
		$this->apiToUrl = $this->apiUrl. "/GetHallSite";
		$this->m_to_para['pAppCode'] = $this->pAppCode;
		
		$this->m_to_para['pCinemaID'] = $para_list['pCinemaID'];
		$this->m_to_para['pHallID']       = $para_list['pHallID'];
		
		$this->m_to_para['pVerifyInfo'] = $this->getMd5Str($this->m_to_para);
		$ret =  $this->post($this->m_to_para);
	
		return $ret;
	}
	
	/**
	 * @param array $para_list
	 * {
	 * 	pFeatureAppNo	String[10字节]	排期编号
	 * }
	 * @return
	 */
	public function getPlanSiteState($para_list = array()) //  4.7  获取对应排期的座位图的状态
	{

		$this->apiToUrl = $this->apiUrl. "/GetPlanSiteState";
		$this->m_to_para['pAppCode'] = $this->pAppCode;
		
		$this->m_to_para['pFeatureAppNo'] = $para_list['pFeatureAppNo'];
		
		$this->m_to_para['pVerifyInfo'] = $this->getMd5Str($this->m_to_para);
		$ret =  $this->post($this->m_to_para);
		
		return $ret;
	}
	
	/**
	 * @param array $para_list
	 * {
	 * 	pCinemaID	String[10字节]	电影院编号
	 *		pPlanDate	String[10字节]	获取排期的日期 格式(yyyy-mm-dd)
	 * }
	 * @return
	 */
	public function realCheckSeatState($para_list = array()) // 4.8  检查需要定票的座位状态情况，并定票锁定座位
	{
		$this->apiToUrl = $this->apiUrl. "/RealCheckSeatState";
		
		$to_para['AppCode'] = $this->pAppCode;
		$to_para['FeatureAppNo'] = $para_list['FeatureAppNo'];
		$to_para['SerialNum'] = $para_list['SerialNum'];
		$to_para['SeatInfos'] = strval(count($para_list['SeatInfos']));
		
		$to_para['PayType'] = $para_list['PayType'];
		$to_para['RecvMobilePhone'] = strval($para_list['RecvMobilePhone']);
		$to_para['VerifyInfo'] = $this->getMd5Str($to_para);
		
		$to_para['SeatInfos'] = $this->xml->xml_encode($para_list['SeatInfos'],'utf-8','root','SeatInfo',false);
		
		//print_r($to_para);
	
		$this->m_to_para['pXmlString'] = $this->xml->xml_encode($to_para,'utf-8','RealCheckSeatStateParameter');
		
// 		print_r($this->m_to_para) ;
// 		exit;
		
		$ret =  $this->post();
		
	
		return $ret;
	}
	
	/**
	 * @param array $para_list
	 * {
	 * 	pCinemaID	String[10字节]	电影院编号
	 *		pPlanDate	String[10字节]	获取排期的日期 格式(yyyy-mm-dd)
	 * }
	 * @return
	 */
	public function sellTicket($para_list = array()) // 4.9   卖常规票(带座位票)
	{
		$this->apiToUrl = $this->apiUrl. "/SellTicket";
		$para_list['AppCode'] = $this->pAppCode;
		$this->m_to_para['pAppCode'] = $this->pAppCode;
		$para_list['VerifyInfo'] = $this->getMd5Str($para_list);
	
		$to_para = $this->xml->xml_encode($para_list);
	
		$to_para['func'] = "RealCheckSeatState";
	
		$ret =  $this->post2($to_para);
	
		return $ret;
	}
	
	/**
	 * @param array $para_list
	 * {
	 * 	pFeatureAppNo	String[10字节]	排期编号
	 * }
	 * @return
	 */
	public function getPlanSiteStatistic($para_list = array())  // 4.10  获取排期座位统计数据
	{
		$this->apiToUrl = $this->apiUrl. "/GetPlanSiteStatistic";
		$this->m_to_para['pAppCode'] = $this->pAppCode;

		$this->m_to_para['pFeatureAppNo']       = $para_list['pFeatureAppNo'];
		
		$ret =  $this->post($this->m_to_para);
	
		return $ret;
	}
	
	/**
	 * @param array $para_list
	 * {
	 * 	pSerialNum	String[20字节]	合作商方定单号（流水号）
	 * }
	 * @return
	 */
	public function getOrderStatus($para_list = array())  // 4.11 查询定单的售票结果
	{
		$this->apiToUrl = $this->apiUrl. "/GetOrderStatus";
		$this->m_to_para['pAppCode'] = $this->pAppCode;
		
		$this->m_to_para['pSerialNum']       = $para_list['pSerialNum'];
		
		$this->m_to_para['pVerifyInfo'] = $this->getMd5Str($this->m_to_para);
		$ret =  $this->post($this->m_to_para);
	
		return $ret;
	}
	
	/**
	 * @param array $para_list
	 * {
	 * 	pFeatureAppNo	String[10字节]	排期编号
	 *		pAppPric	                数值类型	          票面价
	 *     pBalancePric	     数值类型	               确认票面价
	 * }
	 * @return
	 */
	public function setFeaturePrice($para_list = array())  // 4.12  应用商按照协议，设置排期结算价格
	{
		$this->apiToUrl = $this->apiUrl. "/SetFeaturePrice";
		$this->m_to_para['pAppCode'] = $this->pAppCode;
		
		$this->m_to_para['pFeatureAppNo']       = $para_list['pFeatureAppNo'];
		$this->m_to_para['pAppPric']                  = $para_list['pAppPric'];
		$this->m_to_para['pBalancePric']           = $para_list['pBalancePric'];
		
		$this->m_to_para['pVerifyInfo'] = $this->getMd5Str($this->m_to_para);
		$ret =  $this->post($this->m_to_para);
	
		return $ret;
	}
	
	/**
	 * @param array $para_list
	 * {
	 * 	pOrderNO:	String[10字节]	订单号
	 * }
	 * @return
	 */
	public function unLockOrder($para_list = array())  // 4.13  解锁订单座位
	{
		$this->apiToUrl = $this->apiUrl. "/UnLockOrder";
		$this->m_to_para['pAppCode'] = $this->pAppCode;
		
		$this->m_to_para['pOrderNO']                  = $para_list['pOrderNO'];
		
		$this->m_to_para['pVerifyInfo'] = $this->getMd5Str($this->m_to_para);
		$ret =  $this->post($this->m_to_para);
	
		return $ret;
	}
	 
	/**
	 * @param array $para_list
	 * {
	 * 	null
	 * }
	 * @return
	 */
	public function getFeatureFilm($para_list = array())  // 4.14  查询所有影院当前可售排期上映影片
	{
		$this->apiToUrl = $this->apiUrl. "/GetFeatureFilm";
		$this->m_to_para['pAppCode'] = $this->pAppCode;
		
		$this->m_to_para['pVerifyInfo'] = $this->getMd5Str($this->m_to_para);
		$ret =  $this->post($this->m_to_para);
	
		return $ret;
	}
	
	/**
	 * @param array $para_list
	 * {
	 * 	pBeginDate:	String[10字节]	开始日期(yyyy-MM-dd)
	 *		pEndDate	        String[10字节]	结束日期(yyyy-MM-dd)
	 * }
	 * @return
	 */
	public function getOrderInfo($para_list = array())  // 4.15  平台手工工单明细查询接口
	{
		$this->apiToUrl = $this->apiUrl. "/GetOrderInfo";
		$this->m_to_para['pAppCode'] = $this->pAppCode;
		
		$this->m_to_para['pBeginDate']                  = $para_list['pBeginDate'];
		$this->m_to_para['pEndDate']                     = $para_list['pEndDate'];
		
		$this->m_to_para['pVerifyInfo'] = $this->getMd5Str($this->m_to_para);
		$ret =  $this->post($this->m_to_para);
	
		return $ret;
	}
	
	/**
	 * @param array $para_list
	 * {
	 * 	pOrderNO:	    String[10字节]	订单号
	 *		pAppPric	        Double	票面价
	 *		pBalancePric	Double	确认票面价
	 * }
	 * @return
	 */
	public function modifyOrderPrice($para_list = array())  // 4.16  修改订单价格
	{
		$this->apiToUrl = $this->apiUrl. "/ModifyOrderPrice";
		$this->m_to_para['pAppCode'] = $this->pAppCode;
		
		$this->m_to_para['pOrderNO']                      = $para_list['pOrderNO'];
		$this->m_to_para['pAppPric']                         = $para_list['pAppPric'];		
		$this->m_to_para['pBalancePric']                  = $para_list['pBalancePric'];

		$this->m_to_para['pVerifyInfo'] = $this->getMd5Str($this->m_to_para);
		$ret =  $this->post($this->m_to_para);
	
		return $ret;
	}
	
	/**
	 * @param array $para_list
	 * {
	 * 	pFilmNo	String[10字节]	影片编码
	 *		
	 * }
	 * @return
	 */
	public function getFilmCinema($para_list = array())  // 4.17  根据影片编码读取当前上映影院
	{
		$this->apiToUrl = $this->apiUrl. "/GetFilmCinema";
		$this->m_to_para['pAppCode'] = $this->pAppCode;
	
		$this->m_to_para['pFilmNo']                         = $para_list['pFilmNo'];
		
		$this->m_to_para['pVerifyInfo'] = $this->getMd5Str($this->m_to_para);
		$ret =  $this->post($this->m_to_para);
		
		return $ret;
	}
	
	
	/**
	 * @param array $para_list
	 * {
	 * 	pCinemaID	String[10字节]	影院编码
	 *		pFilmNo    	String[10字节]	影片编码
	 *     pPlanDate	String[10字节]	获取排期的日期 格式(yyyy-mm-dd)
	 * }
	 * @return
	 */
	public function getFeatureInfo($para_list = array())  // 4.18  根据影片编码,影院编码读取上映场次排期
	{
		$this->apiToUrl = $this->apiUrl. "/GetFeatureInfo";
		$this->m_to_para['pAppCode'] = $this->pAppCode;
		
		$this->m_to_para['pCinemaID']                      = $para_list['pCinemaID'];
		$this->m_to_para['pFilmNo']                           = $para_list['pFilmNo'];
		$this->m_to_para['pPlanDate']                       = $para_list['pPlanDate'];
		
		$this->m_to_para['pVerifyInfo'] = $this->getMd5Str($this->m_to_para);
		
		$ret =  $this->post($this->m_to_para);
		
		return $ret;
	}
	
	/**
	 * @param array $para_list
	 * {
	 * 	pOrderNO:	String[10字节]	订单号
	 *		pDesc      	String[10字节]	退票原因
	 * }
	 * @return
	 */
	public function backTicket($para_list = array())  // 4.19 退票
	{
		$this->apiToUrl = $this->apiUrl. "/BackTicket";
		$this->m_to_para['pAppCode'] = $this->pAppCode;
		
		$this->m_to_para['pOrderNO']                      = $para_list['pOrderNO'];
		$this->m_to_para['pDesc ']                           = $para_list['pDesc '];
		
		$this->m_to_para['pVerifyInfo'] = $this->getMd5Str($this->m_to_para);
		$ret =  $this->post($this->m_to_para);
	
		return $ret;
	}
	
	/**
	 * @param array $para_list
	 * {
	 * 	pOrderNO:	    String[10字节]	订单号
	 *		pValidCode  	String[10字节]	验证码
	 *		pRequestType	String[2字节]	请求类型0查询订单信息   1通知打票
	 * }
	 * @return
	 */
	public function appPrintTicket($para_list = array())  // 4.20   合作商打票
	{
		$this->apiToUrl = $this->apiUrl. "/AppPrintTicket";
		$this->m_to_para['pAppCode'] = $this->pAppCode;
		
		$this->m_to_para['pOrderNO']                             = $para_list['pOrderNO'];
		$this->m_to_para['pValidCode']                           = $para_list['pValidCode'];
		$this->m_to_para['pRequestType']                       = $para_list['pRequestType'];
		
		$this->m_to_para['pVerifyInfo'] = $this->getMd5Str($this->m_to_para);
		$ret =  $this->post($this->m_to_para);
	
		return $ret;
	}
	
	
	/**
	 * @param array $para_list
	 * {
	 * 	null
	 * }
	 * @return
	 */
	public function getCinemaStatus ($para_list = array())  // 4.21  获取影院连接状态
	{
		$this->apiToUrl = $this->apiUrl. "/GetCinemaStatus ";
		$this->m_to_para['pAppCode'] = $this->pAppCode;
		
		$this->m_to_para['pVerifyInfo'] = $this->getMd5Str($this->m_to_para);
		$ret =  $this->post($this->m_to_para);
	
		return $ret;
	}
	
	
	

	
	

}